Fairness module in dalex

We live in a world that is getting more divided each day. In some parts of the world, the differences and inequalities between races, ethnicities, and sometimes sexes are aggravating. The data we use for modeling is in the major part a reflection of the world it derives from. And the world can be biased, so data and therefore model will likely reflect that. The introduction to this topic is well presented in Fairness and machine learning.

We propose a way in which ML engineers can easily check if their model is biased.

Fairness module is still work-in-progres and new features will be added over time.

Case study - german credit data

To showcase the abilities of the module, we will be using the German Credit Data dataset) to assign risk for each credit-seeker.

This simple task may require using an interpretable decision tree classifier.

We create an Explainer object to proceed with dalex functionalities.

Let's say that performance is satisfying. To check if the model is biased, we will use the fairness module from dalex. Checking if the model is fair should be straightforward. Apart from the dx.Explainer, we will need 2 parameters:

Now it is time to check fairness!

We use a unified dalex interface to create a fairness explanation object. Use the model_fairness() method:

The idea here is that ratios between scores of privileged and unprivileged metrics should be close to 1. The closer the more fair the model is. But to relax this criterion a little bit, it can be written more thoughtfully:

fairness_check

Where the epsilon is a value between 0 and 1, it should be a minimum acceptable value of the ratio. On default, it is 0.8, which adheres to four-fifths rule (80% rule) often looked at in hiring, for example.

This model cannot be called fair! Generally, each metric should be between (epsilon, 1/epsilon). Metrics are calculated for each subgroup, and then their scores are divided by the score of the privileged subgroup. That is why we omit male_old in this method. When at least 2 metrics have scores ratio outside of the epsilon range, the model may be declared unfair. In our case it cannot be decided automatically but the bias is visible and FPR (False Positive Rate) is preety important in case of risk assigning, so let's call our model unfair.

Useful attributes

The result attribute is metric_scores where each row is divided by row indexed with privileged (in this case male_old).

Bias Detection Plots

The fairness explanation object includes plots that allow bias visualization from different perspectives:

  1. fairness_check plot

  2. metric_scores plot

Fairness Check plot

This is a visualization of the fairness_check result.

If a bar reaches the red field, it means that for this metric model is exceeding the (epsilon, 1/epsilon) range. In this case the DecisionTreeClassifier has one NaN. In this case appropriate message is given (it can be disabled with verbose=False).

One can also plot metric scores

Metric Scores plot

This is a visualization of the metric_scores attribute.

Vertical lines showcase the score of the privileged subgroup. Points closer to the line indicate less bias in the model.

Both plot types are complementary in terms of the metrics. Metric Scores plot is an excellent way to ensure that the interpretation of the Fairness Check plot is on point (small metric values may make the ratios high/low).

Multiple models

Supporting multiple model visualization is a key functionality. One can make many models and compare them in terms of the fairness metrics.

When the plot is missing any bars, a console output will tell the user to check the Metric Scores plot. We can examine why these bars are missing:

Parity loss plots

Parity loss plots are other kind of plots that use parity_loss attribute. But what is parity_loss?

Parity loss apart from being an attribute is a way that summarizes the bias across subgroups. We needed a function that is symmetrical in terms of division (f(a/b) = f(b/a)). This is why we decided to use following formula: parityloss

The intuition behind this formula is simple. The bigger the difference in metrics the higher the parity loss will be. It is good entry point for visualization because we have only one value for each metric.

With this knowledge we are now ready for some plots! Note that in all following plots the metrics can be changed. Just pass metrics = ["TPR", "FPR", ... ]

Radar plot

Fairly simple radar plot, it shows each parity loss of metric in form of point on ploar coordinate system.

Judging by the size, the LogisticRegression classifier is the most biased

Heatmap

Heatmap is also simple way to check parity loss.

When Nans are present the fields will be left without color.

Stacked

Stacked plot is a way to look at cumulated parity loss. It stacks each bar on top of each other. The widths of bars depict the amount of bias. The lesser the better.

Performance and Fairness

Sometime it is good idea to look both at performance and fairness metrics. This is where this plot is handy! This plot does not have metrics parameter, but it requires fairness_metric and performance_metric parameters. The dafaults are TPR and accuracy respectively.

Note that y axis is reversed this way the best models are in top right corner.

Ceteris Paribus Cutoff

Last but not least is Ceteris Paribus Cutoff plot. It shows us what would happen if we changed cutoff only for one subgroup. It also shows where minimum of summed parity loss of metrics is.

To achieve minimal parity loss cutoff for female_young should be set to the values shown by minimums. Please note that manipulating cutoff in this way can be considered unfair because we are artificialy lowering standards for particular subgroup.

Mitigation

There are few possible solutions to overcome bias affecting classification models. In dalex, there are 3 mitigation techniques:

  1. resample - returns indices that may be used to pick relevant samples of data

  2. reweight - returns sample (case) weights for model training

  3. roc-pivot - returns the Explainer with changed y_hat

First, let's prepare models by doing copies of Explainers.

Resampling

Reweight

ROC pivot

This function returns an Explainer, but we want to explain the models created before. Next, we make fairness objects.

We see that the metrics are lower after mitigation. We can also observe this in fairness_check, for example let's investigate roc_pivot.

The only issue is in the FPR metric.

Summary

Fairness module in dalex is a unified and accessible way to ensure that the models are fair. In the next versions of the module we plan to add bias mitigation methods. There is a long term plan to add support for individual fairness.

Plots

This package uses plotly to render the plots:

Resources - https://dalex.drwhy.ai/python